<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="handlerOpenDialog(0, 1)">添加权限</el-button>
        </div>
        <el-table row-key="id" border :data="list">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button v-if="row.type === 1" type="text" @click="handlerOpenDialog(row.id, 2)">添加</el-button>
              <el-button type="text" @click="edit(row.id)">编辑</el-button>
              <el-button type="text" @click="handlerDelPer(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :title="isEdit? '编辑权限' : '添加权限'" :visible.sync="perVisible" style="padding-left:50px;" @close="resFrom">
      <el-form ref="addFrom" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" :model="perForm" label-width="100px" :rules="perRules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="perForm.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="perForm.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="perForm.description" />
        </el-form-item>
        <el-form-item>
          <el-row style="padding-top:30px;">
            <el-col :span="10" :offset="6">
              <el-button>取消</el-button>
              <el-button type="primary" @click="submitAddFrom">确定</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionList,
  delPermission as delPermissionAPI,
  getPermissionDetail as getPermissionDetailAPI,
  addPermission as addPermissionAPI,
  updatePermission as updatePermissionAPI
} from '@/api/permissions'
import { getTree } from '@/utils/getTree'
export default {
  name: 'Permissions',
  data() {
    return {
      list: [], // 循环依赖
      perForm: { // 添加表单
        name: '',
        code: '',
        description: '',
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      perRules: { // 添加表单校
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入权限描述', trigger: 'blur' }
        ]
      },
      // dialog 弹层
      perVisible: false,
      // 编辑状态
      isEdit: false
    }
  },
  created() {
    this.getPerList()
  },
  methods: {
    // 点击添加权限
    handlerOpenDialog(pid, type) {
      this.isEdit = false
      this.perForm.type = type
      this.perForm.pid = pid
      this.perVisible = true
    },
    // 初始化数据
    async getPerList() {
      try {
        const res = await getPermissionList()
        // 保存数据
        this.list = getTree(res.data)
      } catch (err) {
        console.log('', err)
      }
    },
    // 删除
    async handlerDelPer(id) {
      try {
        const res = await delPermissionAPI(id)
        this.getPerList()
        // console.log('', res)
        this.$message.success(res.message)
        // 保存数据
      } catch (err) {
        console.log('', err)
      }
    },
    // 点击编辑按钮
    edit(id) {
      this.isEdit = true
      this.perVisible = true
      this.getPermissionDetail(id)
    },
    // 获取权限详情
    async getPermissionDetail(id) {
      console.log('123')
      try {
        const { data } = await getPermissionDetailAPI(id)
        // 保存数据
        this.perForm = data
        // this.perForm =
      } catch (err) {
        console.log('', err)
      }
    },
    // 提交添加表单
    submitAddFrom() {
      this.$refs.addFrom.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.updatePermission() : this.doAdd()
        this.getPerList()
      })
    },
    // 添加权限
    async doAdd() {
      try {
        const res = await addPermissionAPI(this.perForm)
        // 初始化数据
        this.getPerList()
        this.perForm = {}
        this.perVisible = false
        this.$message.success(res.message)
      } catch (err) {
        console.log('', err)
      }
    },
    // 根据id更新权限
    async updatePermission() {
      try {
        const res = await updatePermissionAPI(this.perForm)
        // console.log('id', res)
        this.perForm = {}
        this.perVisible = false
        this.$message.success(res.message)
        // 保存数据
      } catch (err) {
        console.log('', err)
      }
    },
    // 关闭dialog，清空表单
    resFrom() {
      this.$refs.addFrom.resetFields()
    }
  }

}
</script>

<style lang="scss" scoped>

</style>
